<template>
  <div class="login-container">
    <div class="login-card">
      <h1 class="title">欢迎回来</h1>
      <el-form :model="loginForm" @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" :loading="loading">立即登录</el-button>
        </el-form-item>
      </el-form>
      <div class="action-links">
        <router-link to="/register" class="link-text">注册账号</router-link>
        <router-link to="/phone-reset-password" class="forgot-link">忘记密码？</router-link>
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';  // 添加这行
import { authHandler } from '@/utils/authUtils'
import apiClient from '@/utils/request'
import axios from 'axios';

const loginForm = ref({ username: '', password: '' });
const loading = ref(false);
const router = useRouter();  // 添加这行

const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.error('请填写完整信息');
    return;
  }

  loading.value = true; // ✅ 开始加载

  try {
    const response = await axios.post('/api/users/login/', {
      username: loginForm.value.username,
      password: loginForm.value.password,
    })
    // 登录成功
    if (response.data.status === 'success') {
      authHandler.setAuthInfo(
        response.data.access,
        response.data.refresh,
      )
      // 获取用户信息
      const userInfo = await apiClient.get('/api/users/user-info/')
      authHandler.setAuthInfo(userInfo.data)
      ElMessage.success('登录成功')
      window.dispatchEvent(new CustomEvent('login-status-changed', {
        detail: { isLoggedIn: true }
      }))
      router.replace({ name: 'Home' })
    }
  } catch (error) {
    if (error.response) {
      if (error.response.status === 429) {
        ElMessage.warning('请求频率超过限制，请稍后再试')
      } else {
        ElMessage.error(error.response?.data || '登录失败')
      }
    } else {
      console.error(error)
      ElMessage.error('网络错误')
    }
  } finally {
    loading.value = false; // ✅ 结束加载
  }
};

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  padding: 2.5rem;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
  backdrop-filter: blur(10px);
  width: 100%;
  max-width: 420px;
  transform: translateY(-20px);
}

.title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: 600;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

.action-links {
  margin-top: 1.5rem;
  display: flex;
  justify-content: space-between;
}

.link-text {
  color: #666;
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.3s;
}

.link-text:hover {
  color: #4CAF50;
}

@media (max-width: 480px) {
  .login-card {
    padding: 1.5rem;
    margin: 1rem;
  }
  
  .title {
    font-size: 1.5rem;
  }
}
</style>